package com.gzxw.mogublog.xo.service.impl;

import cn.hutool.crypto.digest.MD5;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gzxw.mogublog.commons.entity.User;
import com.gzxw.mogublog.utils.MD5Utils;
import com.gzxw.mogublog.utils.StringUtils;
import com.gzxw.mogublog.xo.mapper.UserMapper;
import com.gzxw.mogublog.xo.service.UserService;
import com.gzxw.mogublog.xo.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    public IPage<User> getList(UserVO userVO) {

        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(userVO.getKeyword())) {
            userQueryWrapper.like("user_name", userVO.getKeyword());
        }
        if (StringUtils.isNotEmpty(userVO.getSource())) {
            userQueryWrapper.eq("source", userVO.getSource());
        }
        if (userVO.getCommentStatus() != null) {
            userQueryWrapper.eq("comment_status", userVO.getCommentStatus());
        }

        if (StringUtils.isNotEmpty(userVO.getOrderByAscColumn())) {
            userQueryWrapper.orderByAsc(StringUtils.underLine(new StringBuffer(userVO.getOrderByAscColumn())).toString());
        } else if (StringUtils.isNotEmpty(userVO.getOrderByDescColumn())) {
            userQueryWrapper.orderByDesc(StringUtils.underLine(new StringBuffer(userVO.getOrderByDescColumn())).toString());
        } else {
            userQueryWrapper.orderByDesc("create_time");
        }

        userQueryWrapper.eq("status", 1);

        Page<User> page = new Page<>();
        page.setCurrent(userVO.getCurrentPage());
        page.setSize(userVO.getPageSize());

        IPage<User> pageList = this.page(page, userQueryWrapper);
        // TODO 获取图片服务

        return pageList;
    }

    @Override
    public String addUser(UserVO userVO) {

        User user = new User();
        // 判断当前user是否存在
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("user_name", userVO.getUserName());
        userQueryWrapper.eq("status", 1);
        int count = this.count(userQueryWrapper);
        if (count > 0) {
            return "当前添加的用户已经存在";
        }
        BeanUtils.copyProperties(userVO, user, "status");
        user.setPassWord(MD5Utils.string2MD5("mogu2018"));
        user.setSource("MOGU");
        user.insert();
        return "添加成功";
    }

    @Override
    public String editUser(UserVO userVO) {
        User user = this.getById(userVO.getUid());
        user.setUserName(userVO.getUserName());
        user.setEmail(userVO.getEmail());
        user.setStartEmailNotification(userVO.getStartEmailNotification());
        user.setOccupation(userVO.getOccupation());
        user.setGender(userVO.getGender());
        user.setQqNumber(userVO.getQqNumber());
        user.setSummary(userVO.getSummary());
        user.setBirthday(userVO.getBirthday());
        user.setAvatar(userVO.getAvatar());
        user.setNickName(userVO.getNickName());
        user.setUserTag(userVO.getUserTag());
        user.setCommentStatus(userVO.getCommentStatus());
        user.setUpdateTime(new Date());
        user.updateById();
        return "更新成功";
    }

    @Override
    public String deleteUser(UserVO userVO) {
        User user = this.getById(userVO.getUid());
        user.setStatus(0);
        user.setUpdateTime(new Date());
        user.updateById();
        return "删除成功";
    }

    @Override
    public String resetUserPassword(UserVO userVO) {
        User user = this.getById(userVO.getUid());
        user.setUpdateTime(new Date());
        user.setPassWord(MD5Utils.string2MD5("mogu2018"));
        this.updateById(user);
        return "重设成功";
    }


}
